﻿using UnityEngine;
using UnityEditor;
using System.Linq;




public class WXTool
{
    //[MenuItem("cframework/WXTool/Export")]
    //public static void DoExport()
    //{
    //    string cdn = null;
    //    string outputPath = null;
    //    string[] args = System.Environment.GetCommandLineArgs();
    //    for (int i = 0; i < args.Length; i++)
    //    {
    //        string arg = args[i];
    //        if (arg == "-cdn")
    //        {
    //            cdn = args[++i];
    //        }
    //        else if(arg == "-output_path")
    //        {
    //            outputPath = args[++i];
    //        }
    //    }

    //    DoExport(outputPath, cdn);
    //}

    public static bool SetAppId(string appId)
    {
        var ass = System.AppDomain.CurrentDomain.GetAssemblies().First((a) => { return a.GetName().Name == "Assembly-CSharp-Editor"; });

        if (string.IsNullOrEmpty(appId))
        {
            return false;
        }

        var t = ass.GetType("WeChatWASM.UnityUtil");
        if (t == null)
        {
            Debug.LogError("type not found: " + "WeChatWASM.UnityUtil");
            return false;
        }

        var m = t.GetMethod("GetEditorConf");
        var a = m.Invoke(null, null);

        var projectConfField = a.GetType().GetField("ProjectConf");
        var p = projectConfField.GetValue(a);

        var appIdField = p.GetType().GetField("Appid");
        appIdField.SetValue(p, appId);
 
        return true;
    }

    public static bool DoExport(string outputPath, string cdn, string appId = null)
    {
        var ass = System.AppDomain.CurrentDomain.GetAssemblies().First((a) => {return a.GetName().Name == "Assembly-CSharp-Editor";});

        {
            var t = ass.GetType("WeChatWASM.UnityUtil");
            if(t == null)
            {
                Debug.LogError("type not found: " + "WeChatWASM.UnityUtil");
                return false;
            }

            var m = t.GetMethod("GetEditorConf");
            var a = m.Invoke(null, null);

            var projectConfField = a.GetType().GetField("ProjectConf");
            var p = projectConfField.GetValue(a);

            var cdnField = p.GetType().GetField("CDN");
            cdnField.SetValue(p, cdn);

            var dstField = p.GetType().GetField("DST");
            dstField.SetValue(p, outputPath);

            if(!string.IsNullOrEmpty(appId))
            {
                var appIdField = p.GetType().GetField("Appid");
                appIdField.SetValue(p, appId);
            }
            
        }

        {
            var t = ass.GetType("WeChatWASM.WXEditorWindow");
            if(t == null)
            {
                Debug.LogError("type not found: " + "WeChatWASM.WXEditorWindow");
                return false;
            }

            var w = System.Activator.CreateInstance(t);

            var enableMethod = w.GetType().GetMethod("OnEnable");
            enableMethod.Invoke(w, null);

            bool buildWebGL = true;
            var doExportMethod = w.GetType().GetMethod("DoExport");
            doExportMethod.Invoke(w, new object[]{buildWebGL});

            Debug.LogError(w);
        }

        return true;
    }
}
